package edu.scau.pro.hotel.controller;

import edu.scau.pro.hotel.entity.CheckInForm;
import edu.scau.pro.hotel.entity.HotelCheckin;
import edu.scau.pro.hotel.entity.HotelRoom;
import edu.scau.pro.hotel.service.IHotelCheckinService;
import edu.scau.pro.hotel.service.IHotelRoomService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
 * (HotelCheckin)表控制层
 *
 * @author CJW
 * @since 2022-03-21 21:03:54
 */
@RestController
@RequestMapping("hotelCheckin")
public class HotelCheckinController {
    /**
     * 服务对象
     */
    @Autowired
    private IHotelCheckinService hotelCheckinService;
    @Autowired
    private IHotelRoomService hotelRoomService;

    /**
     * 分页查询
     *
     * @param hotelCheckin 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public ResponseEntity<List<HotelCheckin>> queryAll(HotelCheckin hotelCheckin) {
        return ResponseEntity.ok(this.hotelCheckinService.queryAll(hotelCheckin));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<HotelCheckin> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.hotelCheckinService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param checkInForm 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<HotelCheckin> add(@RequestBody CheckInForm checkInForm) {
        System.out.println("调用成功");
        return ResponseEntity.ok(this.hotelCheckinService.insert(checkInForm));
    }

    /**
     * 处理入住相关时间和房费
     *
     * @param checkInForm 实例对象
     * @return 实例对象
     */
    @PostMapping("/processCheckinTime")
    public ResponseEntity<CheckInForm> pTimeAndMoney(@RequestBody CheckInForm checkInForm) {
        System.out.println("调用成功");
        return ResponseEntity.ok(this.hotelCheckinService.processTimeAndMoney(checkInForm));
    }

    /**
     * 编辑数据
     *
     * @param hotelCheckin 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<HotelCheckin> edit(@RequestBody HotelCheckin hotelCheckin) {
        return ResponseEntity.ok(this.hotelCheckinService.update(hotelCheckin));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable Integer id) {
        return ResponseEntity.ok(this.hotelCheckinService.deleteById(id));
    }

    @GetMapping("/page")
    public PageInfo page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, HotelCheckin hotelCheckin) {
        PageHelper.startPage(pageNum,pageSize);
        List<HotelCheckin> list = this.hotelCheckinService.queryAll(hotelCheckin);
        System.out.println(hotelCheckin);
        System.out.println(list);
        PageInfo<List> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @GetMapping("/page/room")
    public PageInfo pageByRoom(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, HotelRoom hotelRoom){
        System.out.println(hotelRoom.getRoomNo());
        PageHelper.startPage(pageNum,pageSize);
        List<HotelRoom> roomList = this.hotelRoomService.queryAll(hotelRoom);
        HotelRoom hotelRoomNew = roomList.get(0);
        System.out.println(hotelRoomNew);
        HotelCheckin hotelCheckin = new HotelCheckin();
        hotelCheckin.setRoomId(hotelRoomNew.getRoomId());
        List<HotelCheckin> checkinList = this.hotelCheckinService.queryAll(hotelCheckin);
        PageInfo<List> pageInfo = new PageInfo(checkinList);
        return pageInfo;
    }
}
